package com.novellotus.iov.carvice.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.navi.view.PoiInputResItemWidget;
import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.FailureListAdapter;
import com.novellotus.iov.carvice.bean.FailureBeen;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class FailureListActivity extends BaseActivity implements FailureListAdapter.FailureItemClick,LoadingView.OnBtnClickListener {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.btn_right)
    Button btn_right;
    @BindView(R.id.pageView)
    RecyclerView pageView;
    @BindView(R.id.loadingView)
    LoadingView loadingView;

    FailureListAdapter failureListAdapter;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_failure_list);
    }

    @Override
    public void init() {
        tv_tittle.setText("报修记录");
        pageView.setLayoutManager(MyViewUtils.getLinearLayoutManager(this));
        loadingView.setLoadingModel(LoadingView.MODEL_ALERT);
        loadingView.mWarnView.setTextColor(getResources().getColor(R.color.text_black));
        loadingView.setOnBtnClickListener(this);
        getList();
    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }


    private void getList() {
        Observable<BaseArrayEntity<FailureBeen>> observable = RetrofitFactory.getInstance().fault(SharedPreferencesUtils.getKey(this)[0]);
        observable.compose(RxSchedulers.<BaseArrayEntity<FailureBeen>>lifecycle(this))
                .compose(RxSchedulers.<BaseArrayEntity<FailureBeen>>compose())
                .subscribe(new BaseArrayObserver<FailureBeen>(this) {
                    @Override
                    public void onSuccess(List<FailureBeen> response) {

                        if(response==null||response.size()==0){
                            loadingView.empty("暂无报修记录", false);
                        }else{
                            loadingView.success();
                            failureListAdapter = new FailureListAdapter(response, FailureListActivity.this);
                            pageView.setAdapter(failureListAdapter);
                        }

                    }

                    @Override
                    public void onFail(int code, String msg) {
                        loadingView.empty(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        loadingView.empty("网络请求失败", true);
                    }
                });
    }

    @Override
    public void clickItem(FailureBeen failureBeen) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("FailureBeen", failureBeen);
        openActivity(FailureDetailActivity.class, bundle);
    }

    @Override
    public void onReload() {
        getList();
    }
}
